































































































function findParentByTagName(element, tagName)
{
	var parent = null;
	
	var parentElement = element.parentNode;
	while(parentElement != null)
	{
		if(parentElement.tagName == tagName)
		{
			parent = parentElement;
			break;
		}
		
		parentElement = parentElement.parentNode;
	}
		
	return parent;
}

function refreshResultFields(response)
{
	for(var i = 0 ; i < response.length ; i++)
	{
		var updatedResult = response[i];
		
		var values = document.getElementsByName("result" + updatedResult.valueID);
		for(var j = 0 ; j < values.length ; j++)
			values[j].value = updatedResult.result;
		
		var labelElements = document.getElementsByTagName("label");
		for(var j = 0 ; j < labelElements.length ; j++)
			if(labelElements[j].id.indexOf(updatedResult.valueID) != -1)
				labelElements[j].innerHTML = updatedResult.result;
	}
}

function searchElementInArrayByProperty(propertyName, propertyValue, array)
{
	for(var i = 0 ; i < array.length ; i++)
		if(array[i][propertyName] == propertyValue)
			return array[i];
}

function findElementByColumnID(elements, columnID)
{
	if(elements.length != null)
	{
		for(var i = 0 ; i < elements.length ; i++)
		{
			var parentColumn = findParentByTagName(elements[i], "TD");
			if(parentColumn.id == columnID)
				return elements[i];
		}
	}
}

function getKeyByValue(object, value) 
{
    for(var prop in object) {
        if(object.hasOwnProperty(prop)) {
             if( object[prop] === value )
                 return prop;
        }
    }
}

function getLabelCount()
{
	var fsCase = document.getElementById("fsCase");
	
	var count = 0;
	for(var child = fsCase.firstChild; child != null; child = child.nextSibling) 
		if(child.nodeName == "LABEL")
			count ++;

	return count;
}

function getChildElementByProperty(parentElement, propertyName, propertyValue)
{
	var element = null;
	
	if(parentElement[propertyName] == propertyValue)
		element = parentElement;
	else
	{
		var childNodes = parentElement.childNodes;
		if(childNodes != null)
			for(var i = 0 ; i < childNodes.length ; i++)
			{
				if(childNodes[i][propertyName] == propertyValue)
					element = childNodes[i];
				else
					element = getChildElementByProperty(childNodes[i], propertyName, propertyValue);
					
				if(element != null)
					break;
			}
	}
	
	return element;
}


//TODO: Improve this method -> Currently it is testing if the element already has been inserted. But the same element should be looked up twice. Correct this.
function getChildElementsByProperty(parentElement, propertyName, propertyValue)
{
	var elements = [];
	
	if(parentElement[propertyName] == propertyValue)
		elements.push(parentElement);
	else
	{
		var childNodes = parentElement.childNodes;
		for(var i = 0 ; i < childNodes.length ; i++)
		{
			if(childNodes[i][propertyName] == propertyValue && elements.indexOf(childNodes[i]) == -1) //Bad code...
				elements.push(childNodes[i]);
			var moreElements = getChildElementsByProperty(childNodes[i], propertyName, propertyValue);
				
			if(moreElements.length > 0)
				for(var j = 0 ; j < moreElements.length ; j++)
					if(elements.indexOf(childNodes[i]) == -1) //Bad code...
						elements.push(moreElements[j]);
		}
	}
	
	return elements;
}

/**
 * Method creates a new HTML input field. The input is created with the specified method' parameters.
 * @param inputID The id of the new input.
 * @param inputName The name of the new input.
 * @param isHidden If false, creates a text input otherwise creates a normal text input.
 * @param inputValue The default value, if any, of the new input (This parameter can be omitted when calling the method)
 * @param inputEvent The input event's name (This parameter can be omitted when calling the method)
 * @param inputEventHandler The handler function for the specified event (This parameter can be omitted when calling the method)
 * @returns
 */
function createInputField(inputID, inputName, type, inputValue, inputClass, inputEvent, inputEventHandler)
{
	var newInput = document.createElement("input");
	newInput.setAttribute("id", inputID);
	newInput.setAttribute("name", inputName);
	newInput.setAttribute("value", inputValue);
	newInput.setAttribute("type", type);
	
	if(inputClass != null)
		newInput.setAttribute("class", inputClass);
	
	if(inputEvent != null)
	{
		newInput[inputEvent] = inputEventHandler;
		//newInput.attachEvent(inputEvent, inputEventHandler);
	}
	
	return newInput;
}

function createListBox(lbTooltip, lbItems)
{
	oListBox = new sap.ui.commons.ListBox();
	
	for(var i = 0 ; i < lbItems.length ; i++)
	{
		oItem = new sap.ui.core.ListItem();
		oItem.setText(lbItems[i]);
		oListBox.addItem(oItem);
	}
	
	oListBox.setTooltip(lbTooltip);
	oListBox.setWidth("200px");
	oListBox.setHeight("200px");
	
	return oListBox;
}

function createDialog(dialogTitle, btnAction)
{
	var oDialog1 = new sap.ui.commons.Dialog({modal: true});
	var oBtnCancel = new sap.ui.commons.Button({text: "Cancel", press:closeDialog});
			
	oDialog1.addButton(btnAction);
	oDialog1.addButton(oBtnCancel);

	oDialog1.setTitle(dialogTitle);
	oDialog1.setWidth("250px");	
	oDialog1.setHeight("330px");
	oDialog1.setMinWidth("240px");
	oDialog1.setMinHeight("300px");
	oDialog1.setMaxWidth("350px");
	oDialog1.setMaxHeight("400px");
	oDialog1.setDefaultButton(btnAction);
	
	return oDialog1;
}

function createImageButton(imgID, imgSoruce, imgEventHandler, imgClass)
{
	var imgButton = document.createElement("img");
	imgButton.id = imgID;
	
	imgButton.src = imgSoruce;
	imgButton.onClick = imgEventHandler;
	imgButton.attachEvent("onclick", imgEventHandler);
	
	if(imgClass == null)
		imgButton.setAttribute("class","imgButton");
	else
		imgButton.setAttribute("class", imgClass);
	
	return imgButton;
}